<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/6 0006
 * Time: 21:05
 */
class number implements IteratorAggregate
{
    private $start, $end;


    public function __construct($start, $end)
    {
        $this->start = $start;
        $this->end = $end;
    }


    public function getIterator()
    {
        // TODO: Implement getIterator() method.
        return new number2($this);
    }

    public function getStart()
    {
        return $this->start;
    }

    public function getEnd()
    {
        return $this->end;
    }
}

class number2 implements Iterator
{
    private $cur;
    private $obj;

    public function __construct($obj)
    {
        $this->obj = $obj;
    }


    public function rewind()
    {
        // TODO: Implement rewind() method.
        $this->cur = $this->obj->getStart();
    }

    public function valid()
    {
        // TODO: Implement valid() method.
        return $this->cur <= $this->obj->getEnd();
    }

    public function current()
    {
        // TODO: Implement current() method.
        return pow($this->cur, 2);
    }

    public function key()
    {
        // TODO: Implement key() method.
        return $this->cur;
    }

    public function next()
    {
        // TODO: Implement next() method.
        $this->cur++;
    }


}

$obj = new number(3, 7);

foreach ($obj as $index => $item) {
    echo 'index =' . $index . 'val=' . $item . "\r\n";
}

//IteratorIterator接口把你的类的主要功能和迭代遍历它需要的方法分离到两个独立的实体